<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * @property int create_time
 * @property int refresh_time
 */
class Job extends Model
{
    const UPDATED_AT = null;
    const CREATED_AT = null;
    public $table = 'jobs';

    /*
     *
     * */
    public static function getJobInfo($id = 0)
    {
        $job = Job::find($id);
        if(!$job){
            return [];
        }
        $job->click++;
        $job->save();
        return $job;
    }

    /**
     * @return
     */
    public static function getAllJob()
    {
        $job = Job::where(function ($query, $params) {
            if (isSet($params['company_id'])) {
                $query->where('company_id', $params['company_id']);
            }
            //status 2
            if (isSet($params['status'])) {

            }
            //用户查看
            if (isSet($params['member_status'])) {

            }
        })->get();
        return $job;
    }

    public static function searchResult($params, $category)
    {
        DB::connection()->enableQueryLog();  // 开启QueryLog
        $result = Job::where('display', 1)->where(function ($query) use ($params) {
            if (isSet($params['key'])) {
                $query->where('jobs_name', 'like', '%' . $params['key'] . '%')
                    ->orWhere('company_cn', 'like', '%' . $params['key'] . '%');
            }
        })->where(function ($query) use ($params, $category) {
            if (isSet($params['wage'])) {
                $query->where('negotiable', 1)->orWhere(function ($query) use ($params, $category) {
                    $query->where('negotiable', 0);
                    $category_cn = Category::getCategoryCn($category, '_wage', $params['wage']);
                    $section = explode("~", $category_cn);
                    if (isSet($section[0])) {
                        $star = preg_replace('/[^\w]+/', '', $section[0]);
                        $query->where('min_wage', '>=', $star);
                    }
                    if (isSet($section[1])) {
                        $end = preg_replace('/[^\w]+/', '', $section[1]);
                        $query->where('max_wage', '<=', $end);
                    }
                });
            }
        })->where(function ($query) use ($params, $category) {

            if (isSet($params['trade'])) {
                $trade = Category::select('id')->where('pid', $params['trade'])->where('alias', '_trade')->get()->toArray();
                $trade = array_column($trade, 'id');
                $query->whereIn('category', $trade);
            }
            if (isSet($params['sort'])) {
                if ($params['sort'] == '2') {
                    $query->orderBy('refresh_time', 'desc');
                }
            }
        })->where(function ($query) use ($params) {
            if (isSet($params['c'])) {
                $query->where(['category' => (int)$params['c']]);
            }
        })->where(function ($query) use ($params) {
            if (isSet($params['scale'])) {
                $query->where(['scale' => (int)$params['scale']]);
            }
        });
        $result = $result->paginate(10);
        $result->appends($params);

        return $result;
    }

}
